import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyCustomizableGUI extends JFrame implements ActionListener{

    private JTextField poem = new JTextField("\"The peacock," +
            " Spreading out his tail" +
            " In the spring breeze.\"  Shiki");    
    private JButton preferences = new JButton("Preferences");
    private UserPreferences ups = new UserPreferences();
    
    MyCustomizableGUI(String title){
        super(title);
        GridLayout gr = new GridLayout(2,1);
        setLayout(gr);
        
        FileInputStream fIn = null;
        ObjectInputStream oIn = null;
        
        try{
            fIn = new FileInputStream("D:\\Eclipse\\Cristina\\Workspace\\preferences.ser");
            oIn = new ObjectInputStream(fIn);
            ups = (UserPreferences)oIn.readObject();
        }
        catch (ClassNotFoundException cnf){
            cnf.printStackTrace();
        }
        catch(IOException e){
            
        }
        
        try{
            fIn.close();
            oIn.close();
        }
        catch(IOException ioe){
            ioe.printStackTrace();
        }
        
        System.out.println("Preferences read from "+
                "D:\\Eclipse\\Cristina\\Workspace\\preferences.ser");
        
        add(poem);
        poem.setForeground(ups.colour);
        setTextFont(ups.fontName, ups.fontSize);
        add(preferences);
        preferences.addActionListener(this);
    }
    
    public static void main(String[] args){
        MyCustomizableGUI mcGUI = new MyCustomizableGUI("Lesson 17");
        mcGUI.setSize(400,150);        
        mcGUI.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Preferences preferences = new Preferences(this);
        preferences.setVisible(true);
    }
    
    public void setTextColour(Color c){
        poem.setForeground(c);
    }
    
    public void setTextFont(String fontName, int fontSize){
        Font textFont = new Font(fontName, Font.PLAIN, fontSize);
        poem.setFont(textFont);
    }
    
}

class Preferences extends JDialog implements ActionListener{
    
    private MyCustomizableGUI parent;
    private JButton saveButton;
    private JButton cancelButton;
    private JButton applyButton;
    
    private    Color[]  colours = {Color.MAGENTA, Color.CYAN, Color.PINK, Color.GREEN, Color.ORANGE, Color.YELLOW };
    private String[] stringColours = {"MAGENTA","CYAN","PINK","GREEN","ORANGE","YELLOW"};
    private    JComboBox colourList;
    
    private String[] fontNames;
    private JComboBox fontList;
    
    private int[] fontSizes = {20,22,24,26,28,40,60};
    private String[] stringFontSizes = {"20","22","24","26","28","40","60"};
    private JComboBox fontSizeList;
    
    private    JLabel label1, label2, label3;    

    
    public Preferences(MyCustomizableGUI frame){
        
        super(frame,"Preferences");
        
        parent = frame;
        
        JPanel listPane = new JPanel();
        //listPane.setLayout((LayoutManager) new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
        listPane.setLayout(new GridLayout(3,2));
        label1 = new JLabel("Select Color:");
        label2 = new JLabel("Select Font:");
        label3 = new JLabel("Select Font Size:");
        colourList = new JComboBox(stringColours);
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        fontNames = env.getAvailableFontFamilyNames();
        fontList = new JComboBox(fontNames);
        listPane.add(label1);
        listPane.add(colourList);
        listPane.add(label2);
        listPane.add(fontList);
        listPane.add(label3);
        fontSizeList = new JComboBox(stringFontSizes);
        listPane.add(fontSizeList);
        
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        saveButton = new JButton("Save");
        saveButton.addActionListener(this);
        applyButton = new JButton("Apply");
        applyButton.addActionListener(this);
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));        
        buttonPane.add(cancelButton);
        buttonPane.add(saveButton);
        buttonPane.add(applyButton);
        
        Container contentPane = getContentPane();
        contentPane.add(listPane, BorderLayout.CENTER);
        contentPane.add(buttonPane, BorderLayout.PAGE_END);
        
        pack();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Object source = arg0.getSource();
        if (source == applyButton){
            //System.out.println(colourList.getSelectedIndex());
            parent.setTextColour(colours[colourList.getSelectedIndex()]);
            parent.setTextFont((String) fontList.getSelectedItem(),fontSizes[fontSizeList.getSelectedIndex()]);
        }
        if (source == saveButton){
            UserPreferences ups = new UserPreferences();
            ups.colour = colours[colourList.getSelectedIndex()];
            ups.fontName = (String) fontList.getSelectedItem();
            ups.fontSize = fontSizes[fontSizeList.getSelectedIndex()];
            
            FileOutputStream fOut = null;
            ObjectOutputStream oOut = null;
            
            try{
                fOut = new FileOutputStream("D:\\Eclipse\\Cristina\\Workspace\\preferences.ser");
                oOut = new ObjectOutputStream(fOut);
                oOut.writeObject(ups);
            }
            catch (IOException e){
                e.printStackTrace();
            }    
                
            try{
                    oOut.flush();
                    oOut.close();
                    fOut.close();
            }
            catch(IOException ioe){
                    ioe.printStackTrace();
            }
            
            System.out.println("Preferences saved to D:\\Eclipse\\Cristina\\Workspace\\preferences.ser");
            
        }
        
    }
    
}

class UserPreferences implements java.io.Serializable{
    
    Color colour;
    String fontName;
    int fontSize;
}

